import 'package:flutter/material.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/models/user/xindong_chat_user.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_message_vm.dart';
import 'package:xindong/routes/tab_chats/view/xindong_chat_intimate_cell.dart';
import 'package:xindong/routes/tab_chats/view/xindong_chat_message_cell.dart';
import 'package:xindong/widgets/container/xindong_empty_container.dart';
import 'package:xindong/widgets/container/xindong_getx_builder_container.dart';

class XinDongChatMessageYeMian extends StatefulWidget {
  const XinDongChatMessageYeMian({super.key});

  @override
  State<XinDongChatMessageYeMian> createState() => _XinDongChatMessageYeMianState();
}

class _XinDongChatMessageYeMianState extends State<XinDongChatMessageYeMian> with AutomaticKeepAliveClientMixin {

  // 选择 - 到聊天
  void _onTap(XinDongChatUser model) {
    XinDongPageSingleton.instance.jumpToChatYeMian(userClass: model, fromKongJian: false);
  }

  // 避免状态重置
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return XinDongGetBuilder<XinDongMessageVM>(builder: (viewClass) {
      return XinDongRefreshContainer(
        viewClass: viewClass,
        child: XinDongEmptyContainer(
          dataList: viewClass.dataList,
          emptyTips: "暂无消息，多参与互动聊天吧",
          child: ListView.separated(
            controller: viewClass.scrollController,
            itemCount: viewClass.dataList?.length ?? 0,
            itemBuilder: (BuildContext context, int index) {
              final model = viewClass.dataList![index];
              return XinDongChatMessageCell(
                model: model,
                onTap: _onTap,
                onDelete: (XinDongChatUser model) {},
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return const SizedBox(height: 1.0);
            },
          ),
        ),
      );
    });
  }
}
